package com.atguigu.eduteacher.service.impl;

import com.atguigu.eduteacher.javabean.EduCourse;
import com.atguigu.eduteacher.javabean.EduCourseDescription;
import com.atguigu.eduteacher.javabean.dto.CourseInfoDto;
import com.atguigu.eduteacher.javabean.dto.CoursePublishDto;
import com.atguigu.eduteacher.javabean.dto.CourseQuery;
import com.atguigu.eduteacher.mapper.EduCourseMapper;
import com.atguigu.eduteacher.service.IEduCourseDescriptionService;
import com.atguigu.eduteacher.service.IEduCourseService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper,
        EduCourse> implements IEduCourseService {

    @Autowired
    private IEduCourseDescriptionService courseDescriptionService;

    @Override
    @Transactional
    public String saveCourseInfo(CourseInfoDto courseInfoDto) {
        //保存课程基本信息
        EduCourse course = new EduCourse();
//        course.setStatus(Course.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoDto, course);
        boolean resultCourseInfo = this.save(course);

        if (!resultCourseInfo) {
            throw new GuliException(20001, "课程信息保存失败");
        }

        //保存课程详情信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoDto.getDescription());
        courseDescription.setId(course.getId());
        boolean resultDescription =
                courseDescriptionService.save(courseDescription);
        if (!resultDescription) {
            throw new GuliException(20001, "课程详情信息保存失败");
        }
        return course.getId();
    }

    @Override
    public CourseInfoDto getCourseInfoById(String id) {
        EduCourse eduCourse = baseMapper.selectById(id);
        if (eduCourse == null) {
            throw new GuliException(20001, "数据不存在");
        }
        EduCourseDescription courseDescription =
                courseDescriptionService.getById(id);
        if (courseDescription == null) {
            throw new GuliException(20001, "数据不存在");
        }
        CourseInfoDto courseInfoDto = new CourseInfoDto();
        BeanUtils.copyProperties(eduCourse, courseInfoDto);
        courseInfoDto.setDescription(courseDescription.getDescription());
        return courseInfoDto;
    }

    @Override
    public void updateCourseInfoById(CourseInfoDto courseInfoDto) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoDto, course);
        boolean resultCourseInfo = this.updateById(course);
        if (!resultCourseInfo) {
            throw new GuliException(20001, "课程信息保存失败");
        }
        //保存课程详情信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoDto.getDescription());
        courseDescription.setId(course.getId());
        boolean resultDescription =
                courseDescriptionService.updateById(courseDescription);
        if (!resultDescription) {
            throw new GuliException(20001, "课程详情信息保存失败");
        }
    }

    @Override
    public CoursePublishDto getCoursePublishDtoById(String id) {
        return baseMapper.selectCoursePublishDtoById(id);
    }

    @Override
    public boolean publishCourseById(String id) {
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus(EduCourse.COURSE_NORMAL);
        Integer count = baseMapper.updateById(course);
        return null != count && count > 0;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (courseQuery == null) {
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }
}
